import {RoleFormData, RolePageResult, RoleQueryParam, RoleResourceData,} from '@/types/api/system/role';

import {Option} from '@/types/common';
import request from '@/utils/request';
import {AxiosPromise} from 'axios';
// @ts-ignore
import {AUTH_PATH_WEB_PREFIX} from '@/types/store/prefix.d.ts';

/**
 * 获取角色分页数据
 *
 * @param queryParams
 */
export function listPageRoles(
  queryParams?: RoleQueryParam
): AxiosPromise<RolePageResult> {
  return request({
    url: AUTH_PATH_WEB_PREFIX + '/role/page',
    method: 'get',
    params: queryParams,
  });
}

/**
 * 获取角色下拉数据
 *
 * @param queryParams
 */
export function listSelectRoles(
  queryParams?: RoleQueryParam
): AxiosPromise<Option[]> {
  return request({
    url: AUTH_PATH_WEB_PREFIX + '/role/option',
    method: 'get',
    params: queryParams,
  });
}

/**
 * 获取角色拥有的资源ID集合
 *
 * @param queryParams
 */
export function getRoleResourceIds(roleId: string): AxiosPromise<any> {
  return request({
    url: AUTH_PATH_WEB_PREFIX + '/role/resource/ids',
    method: 'get',
    params: {'roleId': roleId}
  });
}

/**
 * 修改角色资源权限
 *
 * @param queryParams
 */
export function updateRoleMenu(
  roleId: string,
  data: RoleResourceData
): AxiosPromise<any> {
  return request({
    url: AUTH_PATH_WEB_PREFIX + '/role/' + roleId + '/link',
    method: 'put',
    data: data,
  });
}

/**
 * 获取角色详情
 *
 * @param id
 */
export function getRoleFormDetail(id: number): AxiosPromise<RoleFormData> {
  return request({
    url: AUTH_PATH_WEB_PREFIX + '/role/' + id + '/info',
    method: 'get',
  });
}

/**
 * 添加角色
 *
 * @param data
 */
export function addRole(data: RoleFormData) {
  return request({
    url: AUTH_PATH_WEB_PREFIX + '/role',
    method: 'post',
    data: data,
  });
}

/**
 * 更新角色
 *
 * @param id
 * @param data
 */
export function updateRole(id: number, data: RoleFormData) {
  return request({
    url: AUTH_PATH_WEB_PREFIX + '/role/' + id,
    method: 'put',
    data: data,
  });
}

/**
 * 批量删除角色，多个以英文逗号(,)分割
 *
 * @param ids
 */
export function deleteRoles(ids: string) {
  return request({
    url: AUTH_PATH_WEB_PREFIX + '/role/list',
    method: 'delete',
    params: {ids: ids},
  });
}

/**
 * 角色编码下拉列表
 */
export function roleAuthCodeOptions(): AxiosPromise<Option[]> {
  return request({
    url: AUTH_PATH_WEB_PREFIX + '/role/auth-code/option',
    method: 'get',
  });
}

/**
 * 角色数据权限下拉列表
 */
export function roleDataScopeOptions(): AxiosPromise<Option[]> {
  return request({
    url: AUTH_PATH_WEB_PREFIX + '/role/data-scope/option',
    method: 'get',
  });
}

